package com.zdp.algorithm.linkedlist;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 反转部分链表
 * @author Zhengdp
 * @date 2023年02月14日 15:37
 */
public class ReverseSubLinkedList {
    public static void main(String[] args) throws IOException {
        ReverseSubLinkedListImpl impl = new ReverseSubLinkedListImpl();
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        int length = Integer.parseInt(read.readLine());
        String[] s1 = read.readLine().split(" ");
        Node head = Node.createNodeList(s1);
        String[] s2 = read.readLine().split(" ");
        int from = Integer.parseInt(s2[0]);
        int end = Integer.parseInt(s2[1]);
        Node h = impl.reverseSubLinkedList(head,from,end);
        while(h != null){
            System.out.print(h.value + " ");
            h = h.next;
        }
    }
}

class ReverseSubLinkedListImpl {
    public Node reverseSubLinkedList(Node head,int from,int end){
        if(from == end || from > end){
            return head.next;
        }
        int index = 1;
        Node fPre = null;
        Node tmp = head;
        while(index < from) {
            tmp = tmp.next;
            index ++;
        }
        fPre = tmp;
        tmp = reverseNode(tmp.next,from,end);
        if(fPre == null){
            return tmp;
        }else{
            fPre.next = tmp;
            return head.next;
        }
    }
    
    private Node reverseNode(Node head,int from,int end) {
        int length = end - from;
        Node pre = null;
        Node next =null;
        Node cur = head;
        int index = 0;
        while(index <= length){
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
            index++;
        }
        head.next = cur;
        return pre;
    }
}
